package com.example.mp.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "SESSION共享接口")
@RestController
@RequestMapping("/session")
public class SessionController {


    //HttpSession相当于会话通道，进行信息交流
    @ApiOperation("设置session")
    @GetMapping("/set")
    public void set(HttpSession session,
                    @RequestParam("key") String key,
                    @RequestParam("value") Object value) {
        session.setAttribute(key, value);
    }

    @ApiOperation("获取session")
    @GetMapping("/get")
    public Map<String, Object> getAll(HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        Enumeration<String> enumeration = session.getAttributeNames();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            Object value = session.getAttribute(key);
            map.put(key, value);
        }
        return map;
    }
}